package weathercase.subject;

import weathercase.observer.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * 天气数据具体主题
 *
 * @author yuanmenglv on 2019/11/25
 */
public class WeatherDataSubject implements Subject {
    /**
     * 观察者列表
     */
    private List<Observer> observerList;
    /**
     * 温度
     */
    private float temperature;
    /**
     * 湿度
     */
    private float humidity;
    /**
     * 气压
     */
    private float pressure;

    public WeatherDataSubject(int initSize) {
        this.observerList = new ArrayList<>(initSize);
    }


    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if ( observerList.contains(observer) ) {
            observerList.remove(observer);
        }else {
            throw new IllegalArgumentException("传入的观察者不在观察者列表中");
        }
    }

    @Override
    public void notifyObservers() {
        for ( Observer o : observerList ) {
            o.update(temperature, humidity, pressure);
        }
    }

    /**
     * 测量数据发生变化时
     */
    public void measurementsChange(){
        notifyObservers();
    }

    public void setMeasurements(float temperature, float humidity, float pressure){
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChange();
    }






}
